package com.traitswu.websocket.embedtomcat;

import com.google.common.collect.Sets;
import com.traitswu.websocket.ExamplesConfig;
import com.traitswu.websocket.chat.ChatAnnotation;
import com.traitswu.websocket.simple.ClientSimpleEndpoint;
import org.apache.catalina.Context;
import org.apache.catalina.core.NamingContextListener;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.websocket.server.Constants;
import org.apache.tomcat.websocket.server.WsContextListener;
import org.apache.tomcat.websocket.server.WsSci;
import org.junit.Test;

import javax.servlet.ServletContextEvent;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerContainer;
import java.net.URI;

public class EmbedTomcatHttp {


    public static class WsConfig extends WsContextListener {

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            super.contextInitialized(sce);
            ServerContainer sc =
                    (ServerContainer) sce.getServletContext().getAttribute(
                            Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

            try {
                sc.addEndpoint(ChatAnnotation.class);
            } catch (DeploymentException e) {
                throw new IllegalStateException(e);
            }
        }
    }


    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(9080);
        tomcat.setBaseDir("D:\\idea-workspace\\test-websocket\\tmp");
        tomcat.getHost().setAutoDeploy(true);


        String contextPath = "/test-websocket";
//        StandardContext context = new StandardContext();
//        context.setPath(contextPath);
//        context.addLifecycleListener(new Tomcat.FixContextListener());

        Context context = tomcat.addContext(contextPath, null);
//        tomcat.getHost().addChild(context);

        context.addLifecycleListener(new NamingContextListener());

        context.addApplicationListener(WsConfig.class.getName());
//        Tomcat.addServlet(context, "default", new DefaultServlet());
//        context.addServletMappingDecoded("/", "default");
//        WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
        context.addServletContainerInitializer(new WsSci(), Sets.newHashSet(ExamplesConfig.class));

        tomcat.addServlet(contextPath, "homeServlet", new HomeServlet());
        context.addServletMappingDecoded("/test", "homeServlet");

        tomcat.start();
        tomcat.getServer().await();
    }


    @Test
    public void test() throws Exception {
        WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

        Class clazz = ClientSimpleEndpoint.class;
        Session wsSession;
        URI uri = new URI("ws://localhost:8080/test-websocket/simple");
//        if (Endpoint.class.isAssignableFrom(clazz)) {
//            @SuppressWarnings("unchecked")
//            Class<? extends Endpoint> endpointClazz = (Class<? extends Endpoint>) clazz;
//            wsSession = wsContainer.connectToServer(endpointClazz, ClientEndpointConfig.Builder.create().build(), uri);
//        } else {
//            wsSession = wsContainer.connectToServer(clazz, uri);
//        }
//
//        System.out.println(wsSession.getId());


        Session session = wsContainer.connectToServer(clazz, uri);

        System.out.println(session.getId());



    }

}  